﻿using System;
using System.Windows.Forms;
using System.Xml;

using ClassLibrary;

namespace TestProject
{
    public partial class SetChromGeneLink : Form
    {        
        ChromosomeGeneLink link;
        private bool locus=false;
        private decimal amount;
        private int id;
        
        public SetChromGeneLink(ChromosomeGeneLink l, decimal i, int id)
        {
            InitializeComponent();
            link = l;
            amount = i;
            this.id = id;            
        }
        public SetChromGeneLink(ChromosomeGeneLink l, int i)
        {
            InitializeComponent();
            link = l;
            id = i;
            locus = true;
        }
        private void SetChromGeneLink_Load(object sender, EventArgs e)
        {
            name_box.Text = link.name;
            GeneTable gen_t = new GeneTable(link.path_to_gene_table);
            if (!locus)
            {                                
                for (int i = 0; i < amount; i++)
                {
                    Label l = new Label();
                    ComboBox cb = new ComboBox();
                    l.Anchor = AnchorStyles.Left;
                    l.AutoSize = true;
                    l.Text = "Select gene for locus #" + i.ToString();
                    foreach (GeneType g in gen_t.gene_table)
                    {
                        cb.Items.Add(g.name);
                    }
                    tableLayoutPanel1.Controls.Add(l, 0, i);
                    tableLayoutPanel1.Controls.Add(cb, 1, i);
                    if (i < amount - 1)
                    {
                        tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                        tableLayoutPanel1.RowCount++;
                    }
                }
            }
            else
            {
                Label l = new Label();
                ComboBox cb = new ComboBox();
                l.Text = "Select gene for locus";
                foreach (GeneType g in gen_t.gene_table)
                {
                    cb.Items.Add(g.name);
                }
                tableLayoutPanel1.Controls.Add(l, 0, 0);
                tableLayoutPanel1.Controls.Add(cb, 1, 0);
            }
        }

        private void ok_butt_Click(object sender, EventArgs e)
        {
            GeneType g = null;          
            GeneTable gen_t = new GeneTable(link.path_to_gene_table);
            MyXmlDocument doc = new MyXmlDocument();
            doc.LoadXml(System.IO.File.ReadAllText(link.path));
            if (!locus)
            {                
                XmlElement el = doc.CreateElement("chromosome");
                el.SetAttribute("Id", id.ToString());
                el.SetAttribute("LocusesAmount", amount.ToString());
                XmlElement el1 = doc.CreateElement("gene_categories");
                for (int i = 0; i < tableLayoutPanel1.RowCount; i++)
                {

                    ComboBox cb = (ComboBox)tableLayoutPanel1.Controls[1 + 2 * i];
                    if (cb.SelectedItem != null)
                    {
                        foreach (GeneType gg in gen_t.gene_table)
                        {
                            if (gg.name == (string)cb.SelectedItem)
                                g = gg;
                        }
                        el1.InnerText += g.allele_type_id.ToString();
                        if (i < amount - 1)
                            el1.InnerText += " ";
                    }
                    else
                    {
                        MessageBox.Show("Not all locuses were defined. Please define them.");
                        return;
                    }
                }                
                el.AppendChild(el1);
                doc.GetElementsByTagName("chromosomes")[0].AppendChild(el);
                doc.Increment(doc.DocumentElement.Attributes[4]);
            }
            else
            {
                ComboBox cb = (ComboBox)tableLayoutPanel1.Controls[1];
                foreach (XmlNode node in doc.GetElementsByTagName("chromosome"))
                {
                    if (cb.SelectedItem != null)
                    {
                        if (node.Attributes[0].Value == id.ToString())
                        {
                            foreach (GeneType gg in gen_t.gene_table)
                            {
                                if (gg.name == (string)cb.SelectedItem)
                                    g = gg;
                            }
                            node.ChildNodes[0].InnerText += " " + g.allele_type_id.ToString();
                            doc.Increment(node.Attributes[1]);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Locus is undefined. Please define it.");
                        return;
                    }
                }
            }
            doc.Save(link.path);
            this.Close();
        }
    }
}
